package com.wjggy.core.common.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.wjggy.core.common.util.cookie.CookieUtils;
import com.wjggy.core.entities.User;

public class LoginFilter  implements Filter
{
	private static final Logger log = Logger.getLogger(LoginFilter.class);

	/**
	 * Attribute name indicating the filter has been applied to a given request.
	 */
	private final static String FILTER_APPLIED = "request";
	/**
	 * 用户登陆验证
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {		
			if(request.getAttribute("userLogin")!=null&&request.getAttribute("userLogin").equals("true")){
				chain.doFilter(request, response);
			}else{
				User user=CookieUtils.checkUserLogin((HttpServletRequest) request);
				if(user!=null){
					request.setAttribute("userLogin", "true");
					request.setAttribute("user",user);
				}else{
					HttpServletResponse res=(HttpServletResponse) response;
					res.sendRedirect("../login/adminLogin.htm");
				}
				chain.doFilter(request, response);
				
			}
		
	}

	/**
	 * Initializes this filter.
	 * 
	 * @param filterConfig The filter configuration
	 * @throws ServletException If an error occurs
	 */
	public void init(FilterConfig filterConfig) throws ServletException {}

	/**
	 * Destroys this filter.
	 */
	public void destroy() {}
}
